<?php
namespace Home\Controller;

use Think\Controller;
use Zend\Db\Sql\Update;

//登录控制器
class LoginController extends BaseController{
    //显示登录页面
    public function index(){
    	$this->get_header();
    	$this->get_topcategory();
	$this->display();	
    }
    //登录验证
    public function login(){
        $session = session('id');
        if($session){
            //Logout();
            $this->success('您已登录,请退出后再登录','../UserInfo/index');
        }else{
            //1.收集表单信息
            //1.收集表单数据，用户名和密码
            //去除两边空格
            $user = trim($_POST['userName']);
            $pwd = trim($_POST['passWord']);
            
            //单引号转义
            $user = addslashes($user);
            $pwd = addslashes($pwd);
            
            //1 首先验证检验
            $verify_code=$_POST['verify'];
            if (check_code($verify_code)) {
                
                //2 验证用户名密码
                $data =  M('user')->where("user_name='$user' and pwd='$pwd'")->find();
                
                if($data){
                    session('id',$data['user_id']);
                    session('name',$data['user_name']);

                    //登录后更新时间
                    $time = time();
                    $lasttime = M('Userinfo');
                    $value = session('id');
                    $lasttime->where("user_id= $value ")->setField('last_time',$time);
                    
                    $this->success('登录成功','../UserInfo/index');
                    
                } else {
                    
                    $this->error("用户名或密码错误,请重新输入!");
                    
                }
                
            } else {
                
                $this->error("验证码输入错误,请重新输入!");
                
            }
        }
            

    }
    

    //生成验证码
    public function verify(){
        $config =    array(    'fontSize'    =>    14,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    true, // 关闭验证码杂点
        );
        $Verify =     new \Think\Verify($config);
        return  $Verify->entry();
    }
    
    //销毁session
    public function Logout(){
        session(null);
        //跳转
        $this->success('感谢您的支持,记得要时刻关注哦!','../Index/index');
        //$this->redirect('Home/Index/index',array(),'2','感谢您的支持,记得要时刻关注哦!');
    }
    //获取网站信息
    private function get_header(){
    	$webinfomodel=M("Webinfo");
    	$webinfo=$webinfomodel->find(1);
    	$this->assign("webinfo",$webinfo);
    }
    //获取导航
    private function get_topcategory() {
    	$caregorymodel=M("Category");
    	$top_categpry=$caregorymodel->where("parent_id=0")->select();
    	$this->assign("top_categpry",$top_categpry);
    }
    
}